﻿using System;

namespace HRX.PayCenter.Contract
{
    /// <summary>
    /// 结果集
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResultSet<T>
    {
        /// <summary>
        /// Code码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 错误消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 响应数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ResultSet<T> Success(T data = default(T))
        {
            return new ResultSet<T>
            {
                Code = (int)ResultCode.成功,
                Data = data,
                Message = string.Empty
            };
        }

        /// <summary>
        /// 成功或失败
        /// </summary>
        /// <param name="isOk"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultSet<bool> YesOrNo(bool isOk, string message = "")
        {
            return new ResultSet<bool>
            {
                Code = (int)ResultCode.成功,
                Data = isOk,
                Message = isOk ? "" : message
            };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ResultSet<T> Fail(int code, string message, T data = default(T))
        {
            return new ResultSet<T>()
            {
                Code = code,
                Data = data,
                Message = message
            };
        }

        /// <summary>
        /// 系统异常
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ResultSet<T> SysException(string message)
        {
            return new ResultSet<T>()
            {
                Code = (int)ResultCode.系统异常,
                Data = default(T),
                Message = message
            };
        }
    }
}
